package com.sihai.ehr.web.action.salary;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.SalaryStatisticService;
import com.sihai.ehr.web.action.EHRAction;

/**
 * 薪资结算控制器
 * 
 * @author henry
 * 
 */
@Results({ @Result(name = "main", location = "salary_main.jsp"),
		@Result(name = "salary_balance", location = "salary_balance.jsp") })
public class SalaryBalanceAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8545909946931121325L;

	private Log log = LogFactory.getLog(SalaryInsuranceAction.class);
	@BeanInject
	private OrganizationService organizationService;

	/** 薪资统计管理服务　 */
	@BeanInject
	private SalaryStatisticService salaryStatisticService;

	private List<Organization> organizations;
    /**
     * 功能模块
     */
    private String module = "salary_balance";
	/** 显示年月 */
	private int startYear;
	private int endYear;
	private int year;
	private int month;

	@Action("/salary/balance_main")
	@Override
	public String execute() throws Exception {
		listTitle = "薪资结算";
		listAction = "/salary/balance.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		try {
			startYear = NumberUtils.createInteger(PropertiesLoaderUtils
					.loadAllProperties("jdbc.properties").getProperty(
							"attendance_start_year", "2001"));
		} catch (Exception e) {
			startYear = 2001;
		}
		setCurrentYearAndMonth();
		return "main";
	}

	/**
	 * 处理薪资计算
	 * 
	 * @return
	 */
	@Action("/salary/balance")
	public String dispatchBalance() {
		// 加载必须的参数

		return "salary_balance";
	}

	private void setCurrentYearAndMonth() {
		endYear = Calendar.getInstance().get(Calendar.YEAR);
		year = endYear;
		month = Calendar.getInstance().get(Calendar.MONTH);
		if (month == 0) {
			month = 12;
			year = year - 1;
		}
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public String getModule() {
		return module;
	}
	
	public void setModule(String module) {
		this.module = module;
	}
	
	public int getStartYear() {
		return startYear;
	}

	public void setStartYear(int startYear) {
		this.startYear = startYear;
	}

	public int getEndYear() {
		return endYear;
	}

	public void setEndYear(int endYear) {
		this.endYear = endYear;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

}
